#!/bin/bash
# description:　PDF2PDF Auto Install Server
# version:	1.0
# author:	juanjuan_chen
# contact:	juanjuan_chen@foxitsoftware.com
#
#parameter 1: 1 represents only compile Web PDF2PDF and Translation&Conversion App, otherwise all is carried out.

if [ ! -e "/pdf2pdf" ]; then
	mkdir /pdf2pdf
	chown -Rf tomcat.tomcat /pdf2pdf
fi

if [ -z "$TOMCAT_HOME" ]; then
  TOMCAT_HOME=/usr/share/tomcat
fi

if [ ! -d "$TOMCAT_HOME/latest/temp" ]
then
	mkdir $TOMCAT_HOME/latest/temp
	chown -Rf tomcat.tomcat $TOMCAT_HOME/latest/temp
fi

function CompileAndDeployWebPDF2PDF()
{
	echo "##########################################Web PDF2PDF#######################################################"
	#Use of Grunt mergers and compressed file
	local tmpDir="/tmp"
	cp -rf /vagrant/Service/ ${tmpDir}
	cd ${tmpDir}/Service/WebRoot
	if [ $? -ne 0 ]; then
		echo "copy webpdf code............[ERROR]"
		return 1;
	fi



	#Compile And Deploy the war file
	mvn -B -f ${tmpDir}/Service/pom.xml clean install -Pvagrant -DskipTests 
	if [ $? -ne 0 ]; then
		echo "webpdf src compile.......[ERROR]"
		cd ${tmpDir}
		rm -rf ${tmpDir}/Service
		return 1;
	fi

	cd ${tmpDir}
	#Deploy java project to tomcat
	rm -rf ${TOMCAT_HOME}/webapps/PDF2PDF-1.0
	cp -rf ${tmpDir}/Service/target/PDF2PDF-1.0 ${TOMCAT_HOME}/webapps
	if [ $? -ne 0 ]; then
		echo "cp PDF2PDF-1.0.war......[ERROR]"
		rm -rf ${tmpDir}/Service
		return 1;
	fi
	rm -rf ${tmpDir}/Service


	SERVER_FILE=${TOMCAT_HOME}/conf/server.xml
	REPACE='<Host name="local.api.pdf2pdf.com" appBase="webapps/PDF2PDF-1.0" unpackWARs="true" autoDeploy="true"><Context docBase="." path="" /></Host>'
	POSITION='<\/Engine>'
	sed -i "/name=\"local.api.pdf2pdf.com/d"  $SERVER_FILE
	sed -i "/$POSITION/i $REPACE" $SERVER_FILE

	SERVER_FILE=/var/lib/tomcat/webapps/foxit-filesystem-web/WEB-INF/classes/filesystem.xml
	REPACE='<Mountinfo path="/pdf2pdf/"><file><ServerAddress>backup-1ded1114-9001-6dfd-431b-6fadd6e1433</ServerAddress></file></Mountinfo>'
	POSITION='<\/Mountlist>'
	sed -i "/path=\"\/pdf2pdf\/\"/d"  $SERVER_FILE
	sed -i "/$POSITION/i $REPACE" $SERVER_FILE


	SERVER_FILE=/tmp/TB-Schedule/tbschedule-tool/src/main/resources/config.txt
	REPACE='{"rootPath":"/foxit-schedule/cloud-reading","configType":"baseTaskType","name":"PDF2PDFTranslation","value":"{\"baseTaskType\":\"PDF2PDFTranslation\",\"heartBeatRate\":5000,\"judgeDeadInterval\":100000,\"sleepTimeNoData\":500,\"sleepTimeInterval\":0,\"fetchDataNumber\":5,\"executeNumber\":1,\"threadNumber\":1,\"processorType\":\"SLEEP\",\"expireOwnSignInterval\":1.0,\"dealBeanName\":\"jarTaskDeal\",\"taskParameter\":\"type\\u003dpdf2pdf_translation,jarPath\\u003d/usr/share/tomcat/temp/convertranslate-1.0.0.jar\",\"taskKind\":\"static\",\"taskItems\":[\"0:{}\"],\"maxTaskItemsOfOneThreadGroup\":1,\"version\":0,\"sts\":\"resume\"}"}'
	sed -i "/PDF2PDFTranslation/d"  $SERVER_FILE	
	echo "$REPACE">>$SERVER_FILE

	REPACE='{"rootPath":"/foxit-schedule/cloud-reading","configType":"strategy","name":"PDF2PDFTranslation_Strategy","value":"{\"strategyName\":\"PDF2PDFTranslation_Strategy\",\"IPList\":[\"127.0.0.1\"],\"numOfSingleServer\":1,\"assignNum\":1,\"kind\":\"Schedule\",\"taskName\":\"PDF2PDFTranslation\",\"taskParameter\":\"\",\"sts\":\"resume\"}"}'
	sed -i "/PDF2PDFTranslation_Strategy/d"  $SERVER_FILE	
	echo "$REPACE">>$SERVER_FILE

	cd /tmp/TB-Schedule/tbschedule-tool
	mvn clean install
	sudo java -jar /tmp/TB-Schedule/tbschedule-tool/target/tbschedule-tool-0.0.1-SNAPSHOT-jar-with-dependencies.jar

        SERVER_FILE=${TOMCAT_HOME}/webapps/PDF2PDF-1.0/WEB-INF/classes/mongo.xml
	sed -i 's/192.201.103.91:30000,192.201.103.92:30000,192.201.103.93:30000/127.0.0.1:27017/g' $SERVER_FILE
	
	return 0;
}

function CompileAndDeployTranslationConversionApp()
{
	echo "##########################################Translation&Conversion App#######################################################"
	mvn -B -f /vagrant/convertranslate-task/pom.xml clean install -Pvagrant -DskipTests
	if [ $? -ne 0 ]; then
		echo "conversion app src compile.......[ERROR]"
		return 1;
	fi

	cp -rf /vagrant/convertranslate-task/target/convertranslate*.jar ${TOMCAT_HOME}/temp/
	if [ $? -ne 0 ]; then
		echo "cp convertranslate.jar......[ERROR]"
		return 1;
	fi
	
	return 0;
}

function CompileAndDeployConversionLibs()
{
	echo "##########################################Conversion Libs#######################################################"
	sudo /vagrant/vagrant/build.sh  pdf2pdf_fxcore.x64.mt.25581.1599
	if [ $? -ne 0 ]; then
		echo "build Conversion libs code.......[ERROR]"
		return 1;
	fi
	
	return 0;
}

function CompileAndDeployConversionLibs()
{
	echo "##########################################Conversion Libs#######################################################"
	sudo /vagrant/vagrant/build.sh  pdf2pdf_fxcore.x64.mt.25581.1599
	if [ $? -ne 0 ]; then
		echo "build Conversion libs code.......[ERROR]"
		return 1;
	fi
	
	return 0;
}

domainStrArr=("127.0.0.1 local.api.pdf2pdf.com")
for (( i=0; i<1; i++ ))
do
	domainStr=${domainStrArr[$i]}
	retVal=$(cat /etc/hosts | grep "${domainStr}")
	if [ -z "$retVal" ]; then
		echo "${domainStr}" >> /etc/hosts
	fi
done

sudo service tomcat stop


CompileAndDeployWebPDF2PDF
if [ $? -ne 0 ]; then
	exit 1;
fi

CompileAndDeployTranslationConversionApp
if [ $? -ne 0 ]; then
	exit 1;
fi

#if [ "$1" != "1" ]; then
#	CompileAndDeployConversionLibs
#	if [ $? -ne 0 ]; then
#		exit 1;
#	fi
#fi

sudo service tomcat restart
# Copy the vhost conf file to where Apache will find it.
sudo cp /vagrant/vagrant/pdf2pdf.conf /etc/httpd/conf.d/
sudo chmod 644 /etc/httpd/conf.d/pdf2pdf.conf
sudo service httpd restart

